package com.codingclub.application.dto;

import com.codingclub.common.Constatns;
import com.codingclub.common.enums.CategoryType;
import com.codingclub.common.enums.ErrCode;
import com.codingclub.common.exception.BusinessException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 题目分类(SubjectCategory)实体类
 *
 * @author makejava
 * @since 2024-11-06 22:51:20
 */
@Data
public class SubjectCategoryDTO implements Serializable {
    private static final long serialVersionUID = 978899895405706465L;
    /**
     * 主键
     */
    private Long id;
    /**
     * 分类名称
     */
    private String categoryName;
    /**
     * 分类类型
     */
    private Integer categoryType;
    /**
     * 图标链接
     */
    private String imageUrl;
    /**
     * 父级id
     */
    private Long parentId;

    /**
     * 校验修改/删除参数
     * @param dto
     * @param isAdd
     */
    public void validateParam(SubjectCategoryDTO dto,boolean isAdd) {
        if(dto==null){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        if(StringUtils.isAnyBlank(dto.getCategoryName())){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        if(dto.getCategoryType()==null|| !CategoryType.isContainsCode(dto.getCategoryType())){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        if(!isAdd&&dto.getId()==null){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        //如果parentId==null 设置为0
        if(dto.getParentId()==null){
            dto.setParentId(Constatns.ZERO);
        }
    }

    /**
     * 校验查询大分类
     * @param dto
     */
    public void validateQueryParam(SubjectCategoryDTO dto) {
        if(dto.getCategoryType()==null||!CategoryType.isContainsCode(dto.getCategoryType())){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
    }

    /**
     * 校验查询小分类
     * @param dto
     */
    public void validateQueryParam1(SubjectCategoryDTO dto) {
        if(dto.getCategoryType()==null||dto.getParentId()==null){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }
        if(dto.getCategoryType()==null||dto.getCategoryType()!=CategoryType.SMALL_TYPE.code){
            throw new BusinessException(ErrCode.PARAMS_ERROR);
        }

    }
}

